#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > energy_nevpt2_ex.info <<'%EOF%'
   energy_nevpt2_ex
   ----------------
   Molecule:         H2CO
   Wave Functions:   MCSCF (CAS) / NEVPT2 / ano-1 basis
   Test Purpose:     Check NEVPT2 energy calculation for electronic excited state
                     and check freezing of orbitals in NEVPT2
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > energy_nevpt2_ex.dal <<'%EOF%'
ATOMBASIS
 Formaldehyde
 ANO basis
    3    1  Y
       8.     1 Basis=ano-1 4 3 1
O1         0.0978456801            0.0000000000           -0.1036640343
       6.     1 Basis=ano-1 4 3 1
C1        -0.3491426209            0.0000000000            2.4793255835
       1.     1 Basis=ano-1 2 1
H1         0.1257484704            1.7643249913            3.3816764544

EOF

**DALTON INPUT
.RUN WAVE FUNCTION
**WAVE FUNCTIONS
.HF
.MCSCF
.NEVPT2
*SCF INPUT
.DOUBLY OCCUPIED
 6 2
*CONFIGURATION INPUT
.SYMMETRY
 2
.SPIN MUL
 1
.INACTIVE
 4 1
.ELECTRONS
 6
.CAS SPACE
 4 1
*NEVPT2
.FROZEN
 2 0
.THRESHOLD
 0.D0
*OPTIMIZATION
.STATE
 1
.SYM CHECK
-1
.THRESHOLD
 1.D-9
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >energy_nevpt2_ex.check
cat >>energy_nevpt2_ex.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a -i"
else
   GREP="egrep -i"
fi

# Reading from basis set library:
# Wave function:
CRIT1=`$GREP "Number of closed shell electrons * 10" $log | wc -l`
CRIT2=`$GREP "Number of electrons in active shells * 6" $log | wc -l`
CRIT3=`$GREP " active orbitals .* 5" $log | wc -l`
CRIT4=`$GREP "Total number of orbitals .* 46" $log | wc -l`
CRIT5=`$GREP "Spin multiplicity and 2 M_S .* 1 * 0" $log | wc -l`
CRIT6=`$GREP "Total number of symmetries * 2" $log | wc -l`
CRIT7=`$GREP "Reference state symmetry * 2" $log | wc -l`
TEST[1]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6  \+ $CRIT7`
CTRL[1]=7
ERROR[1]="INCORRECT WAVE FUNCTION"

# Symmetry:
CRIT1=`$GREP "Number of configurations * 20" $log | wc -l`
CRIT2=`$GREP "Number of orbital rotations * 227" $log | wc -l`
TEST[2]=`expr $CRIT1  \+ $CRIT2`
CTRL[2]=2
ERROR[2]="CONFIGURATION AND ORBITAL ROTATIONS NOT CORRECT"

# MCSCF energy:
CRIT1=`$GREP "Final MCSCF energy: * \-113\.8516440783.[0-9]" $log | wc -l`
TEST[3]=`expr $CRIT1`
CTRL[3]=1
ERROR[3]="MCSCF ENERGY NOT CORRECT"

# NEVPT2 energy corrections
#old input, without frozen orbitals:
#CRIT1=`$GREP "1 * \-113\.8516440[0-9] * \-114\.11243(59|60)[0-9] * \-114\.1148655[0-9]" $log | wc -l`
CRIT1=`$GREP "1 * \-113\.8516440. * \-114\.1040139. * \-114\.1064429." $log | wc -l`
TEST[4]=`expr $CRIT1`
CTRL[4]=1
ERROR[4]="NEVPT2 energies not correct."

PASSED=1
for i in 1 2 3 4
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
